package com.taptech.doufu.util;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.telephony.TelephonyManager;

/**
 * @author huangzh
 *
 * 2014-3-5
 */
public class NetworkInfoUtil {
	

	
	public static final int NET_TYPE_OFF_LINE = -1;
	public static final int NET_TYPE_WIFI = 0;
	public static final int NET_TYPE_2G = 1;
	public static final int NET_TYPE_3G = 2;
	
	public static Context appCon;

	
	public static boolean networkIsAvailable()
	{
		ConnectivityManager connectMgr = (ConnectivityManager) appCon
		        .getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo info = connectMgr.getActiveNetworkInfo();
		if(null == info)
			return false;	
		return info.isAvailable(); 		
	}

	
	public static  int getNetworkType()
	{
		ConnectivityManager connectMgr = (ConnectivityManager) appCon
		        .getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo info = connectMgr.getActiveNetworkInfo();
		if(null == info)
			return NET_TYPE_OFF_LINE;	
		
		else if(info.getType() == ConnectivityManager.TYPE_WIFI)
			return NET_TYPE_WIFI;
		
		else if(info.getType() == ConnectivityManager.TYPE_MOBILE)
		{
			int subType = info.getSubtype();
			if(subType == TelephonyManager.NETWORK_TYPE_UMTS ||
			   subType == TelephonyManager.NETWORK_TYPE_HSDPA ||			   
			   subType == TelephonyManager.NETWORK_TYPE_EVDO_0 ||
			   subType == TelephonyManager.NETWORK_TYPE_EVDO_A
			  )
			{
				return NET_TYPE_3G;
			}
			else
			{
				return NET_TYPE_2G;
			}			
		}
		return NET_TYPE_OFF_LINE;	
	}
	
}
